//
//  GifViewController.swift
//  DemoUI
// 通过gif文件播放动画
//  Created by 白瑾浩 on 2022/1/10.
//

import UIKit

class GifViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor  = .white
        title = "gif文件播放动画"
        
        let mImageView = UIImageView(frame:CGRect(x: 100, y: 100, width: 200, height: 200))
        self.view.addSubview( mImageView )
        
        
        playGIFOnImageView( name: "pic", imageView: mImageView)
        //webview 加载图片
        let mUIWebView =  UIWebView(frame: CGRect(x: 10, y: 300, width: 200, height: 200))
        self.view.addSubview( mUIWebView )
    
        playGIFOnWebView(name:"pic",webView:mUIWebView)
    
        // Do any additional setup after loading the view.
    }
    //显示gif图片
    func playGIFOnImageView(name:String, imageView:UIImageView){
        
        let path = Bundle.main.path(forResource: name, ofType: "gif")
        let url = URL.init( fileURLWithPath: path! )
        let source = CGImageSourceCreateWithURL( url as CFURL , nil)
        let count = CGImageSourceGetCount( source! )
        var imageArray = Array<UIImage>()
        var imageWidth = Array<Int>()
        var imageHieght = Array<Int>()
        var time : Int = Int()
        for index in 0..<count{
            let image = CGImageSourceCreateImageAtIndex(source!, index, nil)
            imageArray.append( UIImage(cgImage: image! ) )
            //as! 强制转换   as？ 可以通过编译 无值为nil
            let info = CGImageSourceCopyPropertiesAtIndex(source!, index,nil) as! Dictionary<String,AnyObject>
            print( "kCGImagePropertyPixelWidth=\(kCGImagePropertyPixelWidth)")
            let width = Int(truncating: info[kCGImagePropertyPixelWidth as String]! as! NSNumber )
            let height = Int(truncating: info[kCGImagePropertyPixelHeight as String]! as! NSNumber )
            imageWidth.append( width )
            imageHieght.append( height )
            
            print( "width=\(width),height=\(height)")
            
            let timeDic = info[kCGImagePropertyGIFDictionary as String] as! Dictionary<String,AnyObject>
            
            time += Int(truncating: timeDic[kCGImagePropertyGIFDelayTime as String] as! NSNumber)
        }
        imageView.frame = CGRect(x: 0, y: 0, width: imageWidth[0]/3, height: imageHieght[0]/3)
        imageView.animationImages = imageArray
        imageView.animationDuration = TimeInterval(time)
        imageView.animationRepeatCount = 0
        imageView.startAnimating()
    
    }
    //使用webview 加载gif图片
    func playGIFOnWebView(name:String,webView:UIWebView){
        let path = Bundle.main.path(forResource: name, ofType: "gif")
        let url = URL.init( fileURLWithPath: path! )
        
        let imageData = try! Data(contentsOf: url)
        
        webView.scrollView.bounces = false //禁用滚动
        webView.backgroundColor = UIColor.clear//无背景
        webView.scalesPageToFit = true //自适应
        webView.load(imageData, mimeType: "image/gif", textEncodingName: "", baseURL: URL(string: Bundle.main.bundlePath)!)
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
